package org.cjlm.impl;

import com.formdev.flatlaf.util.StringUtils;
import org.cjlm.dao.SuperDao;
import org.cjlm.itf.CompDAO;
import org.cjlm.util.MUID;
import org.cjlm.vo.SuperVO;
import org.cjlm.vo.comp.AttributeVO;
import org.cjlm.vo.comp.CompVO;
import org.cjlm.vo.comp.EntityVO;

import java.util.List;

public class CompDAOImpl implements CompDAO {
    @Override
    public CompVO save(CompVO compVO) throws Exception {
        if(StringUtils.isEmpty(compVO.getPrimaryKeyValue())){
            return insert(compVO);
        }else{
            return update(compVO);
        }
    }

    @Override
    public CompVO insert(CompVO compVO) throws Exception {
        check(compVO);

        compVO.setId(MUID.genID());
        List<SuperVO> entitys = compVO.getChildren();
        if(entitys != null){
            for(SuperVO entity : entitys){
                EntityVO entityVO = (EntityVO) entity;
                entityVO.setId(MUID.genID());
                entityVO.setCompId(compVO.getId());

                List<SuperVO> attributes = entityVO.getChildren();
                if(attributes != null){
                    for(SuperVO attribute : attributes){
                        AttributeVO attributeVO = (AttributeVO) attribute;
                        attributeVO.setId(MUID.genID());
                        attributeVO.setCompId(compVO.getId());
                        attributeVO.setEntityId(entityVO.getId());
                    }
                }
            }
        }

        return new SuperDao().insert(compVO);
    }

    @Override
    public CompVO update(CompVO compVO) throws Exception {
        check(compVO);
        List<SuperVO> entitys = compVO.getChildren();
        if(entitys != null){
            for(SuperVO entity : entitys){
                EntityVO entityVO = (EntityVO) entity;
                entityVO.setCompId(compVO.getId());

                List<SuperVO> attributes = entityVO.getChildren();
                if(attributes != null){
                    for(SuperVO attribute : attributes){
                        AttributeVO attributeVO = (AttributeVO) attribute;
                        attributeVO.setCompId(compVO.getId());
                        attributeVO.setEntityId(entityVO.getId());
                    }
                }
            }
        }

        return new SuperDao().update(compVO);
    }

    @Override
    public CompVO delete(CompVO compVO) throws Exception {
        SuperDao dao = new SuperDao();
        compVO = queryByPk(compVO);
        return dao.delete(compVO);
    }

    @Override
    public List<CompVO> query(CompVO queryVO) throws Exception {
        SuperDao dao = new SuperDao();
        List<CompVO> complist = dao.query(queryVO, "code asc");

        if(complist != null && complist.size() > 0){
            for(CompVO compVO : complist){
                EntityVO qentityVO = new EntityVO(); qentityVO.setCompId(compVO.getPrimaryKeyValue());
                List<SuperVO> entitylist = dao.query(qentityVO);
                if(entitylist != null && entitylist.size() > 0){
                    compVO.setChildren(entitylist);
                    for(SuperVO entityVO : entitylist){
                        AttributeVO qatvo = new AttributeVO(); qatvo.setEntityId(entityVO.getPrimaryKeyValue());
                        List<SuperVO> attributelist = dao.query(qatvo);
                        entityVO.setChildren(attributelist);
                    }
                }
            }
        }

        return complist;
    }

    @Override
    public CompVO queryByPk(CompVO queryVO) throws Exception {
        SuperDao dao = new SuperDao();
        CompVO compVO = dao.queryById(queryVO);
        EntityVO qentityVO = new EntityVO(); qentityVO.setCompId(compVO.getPrimaryKeyValue());
        List<SuperVO> entitylist = dao.query(qentityVO);
        if(entitylist != null && entitylist.size() > 0){
            compVO.setChildren(entitylist);
            for(SuperVO entityVO : entitylist){
                AttributeVO qatvo = new AttributeVO(); qatvo.setEntityId(entityVO.getPrimaryKeyValue());
                List<SuperVO> attributelist = dao.query(qatvo);
                entityVO.setChildren(attributelist);
            }
        }

        return compVO;
    }

    @Override
    public EntityVO save(EntityVO entityVO) throws Exception {
        if(StringUtils.isEmpty(entityVO.getPrimaryKeyValue())){
            return insert(entityVO);
        }else{
            return update(entityVO);
        }
    }

    @Override
    public EntityVO insert(EntityVO entityVO) throws Exception {
        check(entityVO);
        entityVO.setId(MUID.genID());

        List<SuperVO> attributes = entityVO.getChildren();
        if(attributes != null){
            for(SuperVO attribute : attributes){
                AttributeVO attributeVO = (AttributeVO) attribute;
                attributeVO.setId(MUID.genID());
                attributeVO.setCompId(entityVO.getCompId());
                attributeVO.setEntityId(entityVO.getId());
            }
        }
        return new SuperDao().insert(entityVO);
    }

    @Override
    public EntityVO update(EntityVO entityVO) throws Exception {
        check(entityVO);
        List<SuperVO> attributes = entityVO.getChildren();
        if(attributes != null){
            for(SuperVO attribute : attributes){
                AttributeVO attributeVO = (AttributeVO) attribute;
                attributeVO.setCompId(entityVO.getCompId());
                attributeVO.setEntityId(entityVO.getId());
            }
        }
        return new SuperDao().update(entityVO);
    }

    @Override
    public EntityVO delete(EntityVO entityVO) throws Exception {
        SuperDao dao = new SuperDao();
        entityVO = queryByPk(entityVO);
        return dao.delete(entityVO);
    }

    @Override
    public List<EntityVO> query(EntityVO queryVO) throws Exception {
        SuperDao dao = new SuperDao();
        List<EntityVO> entitylist = dao.query(queryVO, "isMain desc, code asc");
        if(entitylist != null && entitylist.size() > 0){
            for(SuperVO entityVO : entitylist){
                AttributeVO qatvo = new AttributeVO(); qatvo.setEntityId(entityVO.getPrimaryKeyValue());
                List<SuperVO> attributelist = dao.query(qatvo);
                entityVO.setChildren(attributelist);
            }
        }

        return entitylist;
    }

    @Override
    public EntityVO queryByPk(EntityVO queryVO) throws Exception {
        SuperDao dao = new SuperDao();
        EntityVO entityVO = dao.queryById(queryVO);
        AttributeVO qatvo = new AttributeVO(); qatvo.setEntityId(entityVO.getPrimaryKeyValue());
        List<SuperVO> attributelist = dao.query(qatvo);
        entityVO.setChildren(attributelist);
        return entityVO;
    }

    @Override
    public AttributeVO save(AttributeVO attributeVO) throws Exception {
        if(StringUtils.isEmpty(attributeVO.getPrimaryKeyValue())){
            return insert(attributeVO);
        }else{
            return update(attributeVO);
        }
    }

    @Override
    public AttributeVO insert(AttributeVO attributeVO) throws Exception {
        check(attributeVO);
        attributeVO.setId(MUID.genID());
        return new SuperDao().insert(attributeVO);
    }

    @Override
    public AttributeVO update(AttributeVO attributeVO) throws Exception {
        check(attributeVO);
        return new SuperDao().update(attributeVO);
    }

    @Override
    public AttributeVO delete(AttributeVO attributeVO) throws Exception {
        return new SuperDao().delete(attributeVO);
    }

    @Override
    public List<AttributeVO> query(AttributeVO queryVO) throws Exception {
        return new SuperDao().query(queryVO, "isPriKey desc, code asc");
    }

    @Override
    public AttributeVO queryByPk(AttributeVO queryVO) throws Exception {
        return new SuperDao().queryById(queryVO);
    }

    private void check(CompVO compVO) throws Exception{
        if(StringUtils.isEmpty(compVO.getCode())){
            throw new Exception("业务组件编码不能为空");
        }else if(StringUtils.isEmpty(compVO.getName())){
            throw new Exception("业务组件名称不能为空");
        }
    }

    private void check(EntityVO entityVO) throws Exception{
        if(StringUtils.isEmpty(entityVO.getCode())){
            throw new Exception("实体编码不能为空");
        }else if(StringUtils.isEmpty(entityVO.getName())){
            throw new Exception("实体组件名称不能为空");
        }
    }

    private void check(AttributeVO attributeVO) throws Exception{
        if(StringUtils.isEmpty(attributeVO.getCode())){
            throw new Exception("字段编码不能为空");
        }else if(StringUtils.isEmpty(attributeVO.getName())){
            throw new Exception("字段名称不能为空");
        }else if(StringUtils.isEmpty(attributeVO.getName())){
            throw new Exception("字段类型不能为空");
        }
    }
}
